package com.yuandengta.yomall.product.service.impl;

import com.yuandengta.yomall.product.entity.AttrEntity;
import com.yuandengta.yomall.product.service.AttrService;
import com.yuandengta.yomall.product.vo.AttrGroupWithAttrsVO;
import com.yuandengta.yomall.product.vo.SkuItemVO;
import com.yuandengta.yomall.product.vo.SpuAttrGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.product.dao.AttrGroupDao;
import com.yuandengta.yomall.product.entity.AttrGroupEntity;
import com.yuandengta.yomall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

/**
 *
 * 属性分组接口实现 {@link AttrGroupService}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据三级分类id查询属性分组
     * @param params 封装了分页信息参数
     * @param catelogId 三级分类id
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        // 如果三级分类id为0，则查询所有属性分组
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );

            return new PageUtils(page);
        } else {
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );

            return new PageUtils(page);
        }
    }

    /**
     * 获取分类下所有属性分组及关联的所有属性
     * @param catelogId 三级分类ID
     * @return 分组及关联的所有属性集合
     */
    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 获取分类的所有属性分组
        List<AttrGroupEntity> attrGroupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>()
                        .eq("catelog_id", catelogId));

        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVO);
            // 获取分组的所有属性
            List<AttrEntity> attrEntities = attrService.getAttrRelation(attrGroupWithAttrsVO.getAttrGroupId());
            attrGroupWithAttrsVO.setAttrs(attrEntities);
            return attrGroupWithAttrsVO;
        }).collect(Collectors.toList());

        return attrGroupWithAttrsVOS;
    }

    /**
     * 获取商品的所有属性分组及关联的所有属性
     * @param catalogId 三级分类id
     * @param spuId
     * @return
     */
    @Override
    public List<SpuAttrGroupVO> getAttrGroupWithAttrsBySpuId(Long catalogId, Long spuId) {
        List<SpuAttrGroupVO> vos = baseMapper.selectAttrGroupWithAttrsBySpuId(catalogId, spuId);
        return vos;
    }

}